package io.renren.modules.media.ffmpeg;


import lombok.extern.log4j.Log4j2;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import org.bytedeco.javacv.FFmpegFrameGrabber;

import io.renren.common.utils.MD5Util;
import io.renren.modules.media.model.VideoInfo;

/**
 * 获取视频信息
 * **/
@Log4j2
public class GetVideoInfo {
    public static VideoInfo getVideoInfo(InputStream inputStream){
        VideoInfo info = new VideoInfo();
        FFmpegFrameGrabber grabber = null;
        try {
            grabber = new FFmpegFrameGrabber(inputStream);
            grabber.start();

            int lengthInFrames = grabber.getLengthInVideoFrames();
            double frameRate = grabber.getVideoFrameRate();
            double duration = grabber.getLengthInTime() / 1000000.00;
            int width = grabber.getImageWidth();
            int height = grabber.getImageHeight();
            int audioChannel = grabber.getAudioChannels();
            String videoCode = grabber.getVideoCodecName();
            String audioCode = grabber.getAudioCodecName();
            String md5 = MD5Util.getMD5ByInputStream(inputStream);
            int sampleRate = grabber.getSampleRate();

            info.setLengthInFrames(lengthInFrames);
            info.setFrameRate(frameRate);
            info.setDuration(duration);
            info.setWidth(width);
            info.setHeight(height);
            info.setAudioChannel(audioChannel);
            info.setMd5(md5);
            info.setVideoCode(videoCode);
            info.setAudioCode(audioCode);
            info.setSampleRate(sampleRate);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (grabber != null) {
                    grabber.stop();
                    grabber.release();
                }
            } catch (FFmpegFrameGrabber.Exception e) {
                log.error("getVideoInfo grabber.release failed");
            }
        }
        return info;
    }
    
    public static VideoInfo getVideoInfoByFile(File file){
        VideoInfo info = new VideoInfo();
        FFmpegFrameGrabber grabber = null;
        try {
            grabber = new FFmpegFrameGrabber(file);
            grabber.start();

            int lengthInFrames = grabber.getLengthInVideoFrames();
            double frameRate = grabber.getVideoFrameRate();
            double duration = grabber.getLengthInTime() / 1000000.00;
            int width = grabber.getImageWidth();
            int height = grabber.getImageHeight();
            int audioChannel = grabber.getAudioChannels();
            String videoCode = grabber.getVideoCodecName();
            String audioCode = grabber.getAudioCodecName();
            String md5 = MD5Util.getMD5ByInputStream(new FileInputStream(file));
            int sampleRate = grabber.getSampleRate();

            info.setLengthInFrames(lengthInFrames);
            info.setFrameRate(frameRate);
            info.setDuration(duration);
            info.setWidth(width);
            info.setHeight(height);
            info.setAudioChannel(audioChannel);
            info.setMd5(md5);
            info.setVideoCode(videoCode);
            info.setAudioCode(audioCode);
            info.setSampleRate(sampleRate);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (grabber != null) {
                    grabber.stop();
                    grabber.release();
                }
            } catch (FFmpegFrameGrabber.Exception e) {
                log.error("getVideoInfo grabber.release failed");
            }
        }
        return info;
    }
}
